/*
 * DBClient.java
 *
 * Created on 21 de Outubro de 2007, 16:20
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package Remote;

import Util.Observer;
import java.io.IOException;
import java.util.Vector;

/**
 *
 * @author Ronaldinho
 */
public class DBClient extends Connectable {
    
    private Observer observer;
    private String serverIp;
    
    /** Creates a new instance of DBClient */
    public DBClient(int receivePort, int sendPort, String serverIp, Observer observer) throws IOException {
        super(receivePort, sendPort);
        this.observer = observer;
        this.serverIp = serverIp;
    }
    
    public void acessarBD(Vector<Object> info) throws IOException{
        this.enviarMensagem(this.serverIp, new Message(info, false));
    }
    
    protected void conectar() {
        try {
            this.receberMensagem();
            //conte�do da mensagem
            Vector<Object> info = this.message.info;
            this.connectionEnded = this.message.end;
            if(!this.connectionEnded)
            {
                this.observer.observe(info);           
            }
            
        } catch (IOException ex) {
            ex.printStackTrace();
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        }
    }
    
    public void run() {
        System.out.println("start DBClient");
        while(!this.connectionEnded){
            this.conectar();
        }
        System.out.println("end DBClient");
    }
}
